{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n",
    "\n",
    "https://www.cnblogs.com/pinard\n",
    "\n",
    "Permission given to modify the code as long as you keep this declaration at the top\n",
    "\n",
    "用scikit-learn学习谱聚类 https://www.cnblogs.com/pinard/p/6235920.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "X, y = datasets.make_blobs(n_samples=500, n_features=6, centers=5, cluster_std=[0.4, 0.3, 0.4, 0.3, 0.4], random_state=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calinski-Harabasz Score 14910.411255265064\n"
     ]
    }
   ],
   "source": [
    "from sklearn.cluster import SpectralClustering\n",
    "y_pred = SpectralClustering().fit_predict(X)\n",
    "from sklearn import metrics\n",
    "print (\"Calinski-Harabasz Score\", metrics.calinski_harabaz_score(X, y_pred)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 3 score: 1979.7709609161868\n",
      "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 4 score: 3154.0184121901607\n",
      "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 5 score: 23410.638949991386\n",
      "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 6 score: 19296.861797427086\n",
      "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 3 score: 1979.7709609161868\n",
      "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 4 score: 3154.0184121901607\n",
      "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 5 score: 23410.638949991386\n",
      "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 6 score: 19427.961894359105\n",
      "Calinski-Harabasz Score with gamma= 1 n_clusters= 3 score: 260.64607765944106\n",
      "Calinski-Harabasz Score with gamma= 1 n_clusters= 4 score: 2274.7475744437675\n",
      "Calinski-Harabasz Score with gamma= 1 n_clusters= 5 score: 23410.638949991386\n",
      "Calinski-Harabasz Score with gamma= 1 n_clusters= 6 score: 1575.424687869317\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\manifold\\spectral_embedding_.py:234: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.\n",
      "  warnings.warn(\"Graph is not fully connected, spectral embedding\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calinski-Harabasz Score with gamma= 10 n_clusters= 3 score: 26.939459595237867\n",
      "Calinski-Harabasz Score with gamma= 10 n_clusters= 4 score: 37.011003925097825\n",
      "Calinski-Harabasz Score with gamma= 10 n_clusters= 5 score: 31.922080624500378\n",
      "Calinski-Harabasz Score with gamma= 10 n_clusters= 6 score: 28.434790532434917\n"
     ]
    }
   ],
   "source": [
    "for index, gamma in enumerate((0.01,0.1,1,10)):\n",
    "    for index, k in enumerate((3,4,5,6)):\n",
    "        y_pred = SpectralClustering(n_clusters=k, gamma=gamma).fit_predict(X)\n",
    "        print (\"Calinski-Harabasz Score with gamma=\", gamma, \"n_clusters=\", k,\"score:\", metrics.calinski_harabaz_score(X, y_pred)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calinski-Harabasz Score 14950.49397167252\n"
     ]
    }
   ],
   "source": [
    "y_pred = SpectralClustering(gamma=0.1).fit_predict(X)\n",
    "print (\"Calinski-Harabasz Score\", metrics.calinski_harabaz_score(X, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
